package com.jcl.recruit.api.searchresume.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jcl.core.common.page.PageResult;
import com.jcl.core.resp.ApiResponse;
import com.jcl.recruit.core.web.utils.RequestUtils;
import com.jcl.recruit.resume.service.ResumeAttentionService;
import com.jcl.recruit.resume.service.ResumeBaseService;
import com.jcl.recruit.searchresume.vo.ResumeSerachVo;
import com.jcl.recruit.user.model.UserResumeAttention;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static org.springframework.beans.support.PagedListHolder.DEFAULT_PAGE_SIZE;

@RestController
@RequestMapping("/api")
public class SearchResumeAttentionController {

    @Autowired
    private ResumeAttentionService resumeAttentionService;

    @Autowired
    private ResumeBaseService resumeService;

    /**
     * 关注简历
     * @param searchVo
     * @return
     */
    @RequestMapping("/searchresume/attention/find")
    public ApiResponse findAttentionResume(@RequestBody ResumeSerachVo searchVo){

        Integer userId = Integer.parseInt(RequestUtils.getUserId());
        List<UserResumeAttention> attentionList = resumeAttentionService.getListByUserId(userId);

        if(org.springframework.util.CollectionUtils.isEmpty(attentionList)){
            PageResult result = new PageResult(searchVo.getToPage(), new ArrayList<>());
            return ApiResponse.success(result);
        }else{
            List<Integer> resumeIds = new ArrayList<>();
            for(UserResumeAttention attention: attentionList){
                resumeIds.add(attention.getResumeId());
            }
            PageHelper.startPage(searchVo.getToPage(), DEFAULT_PAGE_SIZE);
            List<Map<String,Object>> searchResumes = resumeService.selectAttentionResumesByCondition(resumeIds);
            PageInfo pageInfo = new PageInfo(searchResumes);
            PageResult result = new PageResult(searchVo.getToPage(), null, pageInfo.getTotal(), pageInfo.getList());
            return ApiResponse.success(result);
        }
    }

}
